package com.pengli.mytools.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.pengli.mytools.R;
import com.pengli.mytools.beans.po.URLData;

import java.util.List;

public class HomeGridAdapter extends BaseAdapter {
    private Context context;
    private List<URLData> list;

    public HomeGridAdapter(Context context, List<URLData> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_gridlayout_home, null);
            viewHolder = new MyViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.item_grid_img);
            viewHolder.textView = convertView.findViewById(R.id.item_grid_textview);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        URLData urlData = list.get(position);
        viewHolder.textView.setText(urlData.getUrlName());

        //新版Glide(4.6.0 之后)  需要设置options
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.default_img_bg)
                .error(R.mipmap.error)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(context).load(urlData.getUrlIconImgUrl()).apply(options).into(viewHolder.imageView);

        return convertView;
    }

    private class MyViewHolder {
        private ImageView imageView;
        private TextView textView;

    }
}
